﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;

public partial class ReqOutManager : PMSBase
{ 
    private SqlCodeConfig codeConfig;
    string cmdText = "SELECT T.*,A.CodeName AS RequisitionTypeName" + PMSAccess.AppendSql_UserOrg + " FROM {0} T LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.RequisitionType AND A.Category='" + PMSCodeCategory.PMS_CKLX + "' where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "IC_Requisition";
        this.Key = "RequisitionID";
        if (!IsPostBack)
        { 
            string sql = string.Format(cmdText+" ORDER BY T.CREATETIME DESC", this.TableName);
            FillGirdView(0, sql);
            InitLoadData();
            if (!string.IsNullOrEmpty(Request["RequisitionID"]))
            {
                this.btnView_Click(null, e);
            }
            tbGoodCode.Attributes.Add("onkeypress", "Openfree.prototype.EnterSubmit('" + tbGoodCode.ClientID + "',event);");
            P_GetUser.Attributes.Add("onkeypress", "Openfree.prototype.EnterSubmit('" + P_GetUser.ClientID + "',event);");
        }
        else
        {
            if (pnlEdit.Visible)
            {
                string cmd = "select top 1 UserName from PMS_User where Barcode='" + P_GetUser.Text.Trim()+ "'";
                string str = PMSAccess.ExecuteScalar(this.Connection,cmd);
                lbGetUserName.Text = str;

                if (!string.IsNullOrEmpty(tbGoodCode.Text))
                {
                    GoodItemOut.AddSelectCode(tbGoodCode.Text);
                    tbGoodCode.Text = "";
                    tbGoodCode.Focus();
                }
            }
            else if (pnlGrid.Visible)
                FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    public void btnSearchGoodCode_Click(object sender, EventArgs e)
    {
        pnlSearchGoodCode.Visible = true;
    }
    private void InitLoadData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_CKLX);
        this.BindDropDownList(S_RequisitionType, ds, true);
        this.BindDropDownList(P_RequisitionType, ds, true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format(cmdText, this.TableName));

        if (!string.IsNullOrEmpty(S_TaskCode.Text))
        {
            sql.Append(" AND T.TaskID in (select TaskID from PMS_TaskItem where TaskCode='" + S_TaskCode.Text.Trim() + "') ");
        }
        if (!string.IsNullOrEmpty(S_RequisitionCode.Text))
        {
            sql.Append(" AND T.RequisitionCode like '%").Append(S_RequisitionCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_RequisitionDate.Text))
        {
            sql.Append(" AND T.RequisitionDate = '").Append(S_RequisitionDate.Text).Append("'");
        }
        if (!string.IsNullOrEmpty(S_RequisitionType.SelectedValue))
        {
            sql.Append(" AND T.RequisitionType = '").Append(S_RequisitionType.SelectedValue).Append("'");
        }
        sql.Append(" ORDER BY T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "ReqOutManager.aspx?RequisitionID=" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
            e.Row.Cells[3].Text = this.ParseDateToShort(e.Row.Cells[3].Text);
            e.Row.Cells[6].Text = this.ParseDateToShort(e.Row.Cells[6].Text);
            e.Row.Cells[9].Text = PMSGoodsState.GetName(e.Row.Cells[9].Text);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }
     
    /// <summary>
    /// 清空控件，清空指定的TextBox、Label
    /// </summary>
    private void ResetComponent()
    { 
        this.ClearPrexControlValues(this.DefaultPrex);
        pnlGrid.Visible = false;
        pnlEdit.Visible = false;
        pnlTaskInfo.Visible = false;
        btnSelect.Visible = true;
        btnSave.Visible = true;
        btnGetOut.Visible = true;
        lbGetUserName.Text = "";
        P_LoginName.Text = SessionManager.Username;
        P_Department.Text = SessionManager.OrgName;
        P_GetDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
        P_LoginName.ReadOnly = true;
        P_Department.ReadOnly = true;
        btnSearchGoodCode.Visible = true;
        pnlSearchGoodCode.Visible = false;
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        hfAddOrEdit.Value = "Add";
        ResetComponent();
        pnlEdit.Visible = true; 
        SetReadOnly(this.DefaultPrex, false);
        P_RequisitionDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        DataSet ds = PMSAccess.GetDataSet(this.Connection,"select * from " + this.TableName + " where 1=2 ", this.TableName);
        SessionManager.DataSet = ds;
        GoodItemOut.IsReadOnly = false;
        GoodItemOut.InitDataOut("IC_RequisitionItem", "RequisitionItemID", "RequisitionID", System.Guid.NewGuid().ToString());
    }


    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string reqId = Request.Form["RequisitionID"];
        if (string.IsNullOrEmpty(reqId))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, reqId);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0];
        int status = Convert.ToInt32(dr["Status"]);
        if (status == PMSGoodsState.IsOut)
        {
            Alert("您选择的申请单已经签发出库，不能再进行操作！");
            return;
        }
        base.WriteLog("领料单", "删除领料单'" + reqId + "'!",
            LogType.Warning, this.GetType().ToString());
        PMSAccess.RemoveDataRow(this.TableName, this.Key, reqId);
        PMSAccess.RemoveDataRow("IC_RequisitionItem", this.Key, reqId);
        //如果是日常零修任务的出库申请则改变库房状态
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }


    protected void btnEdit_Click(object sender, EventArgs e)
    { 
        string reqId = Request.Form["RequisitionID"];
        if (string.IsNullOrEmpty(reqId))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        hfAddOrEdit.Value = "Edit";
        hfRequisitionID.Value = reqId;
        string sql = string.Format(cmdText + " AND T.RequisitionID='" + reqId + "' ", this.TableName);
        DataSet ds = PMSAccess.GetDataSet(this.Connection,sql,this.TableName);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0];
        int status = Convert.ToInt32(dr["Status"]);
        if (status == PMSGoodsState.IsOut)
        {
            Alert("您选择的申请单已经签发出库，不能再进行操作！");
            return;
        }
        ResetComponent(); 
        pnlEdit.Visible = true; 
        SetReadOnly(this.DefaultPrex, false);
        P_LoginName.ReadOnly = true;
        P_Department.ReadOnly = true;
        SessionManager.DataSet = ds;
        this.FillValueByPrexFromDataRow(dr, this.DefaultPrex);
        P_LoginName.Text = dr["UserName"].ToString();
        P_Department.Text = dr["OrgName"].ToString();

        GoodItemOut.IsReadOnly = false;
        GoodItemOut.InitDataOut("IC_RequisitionItem", "RequisitionItemID", "RequisitionID", reqId);
        //显示受理信息 
        DataSet tempds = PMSAccess.GetDataSet(this.Connection, "select * from PMS_TaskLX where TaskID in(select TaskID from PMS_TaskItem where TaskItemId='" + dr["TaskID"].ToString() + "')");
        if (tempds != null && tempds.Tables[0].Rows.Count > 0)
        {
            pnlTaskInfo.Visible = true;
            DataRow row = tempds.Tables[0].Rows[0];
            UserAccess user = new UserAccess(base.Connection);
            tbGuest.Text = row["Guest"].ToString();
            tbRoomNo.Text = row["RoomNo"].ToString();
            cbHouseNo.Text = row["HouseNo"].ToString();
            tbPhone.Text = row["Phone"].ToString();
            tbTroubleDescription.Text = row["TroubleDescription"].ToString();
            lbOrgID.Text = user.GetUserOrgName(row["OrgID"].ToString());
            lbUserID.Text = user.GetUserNameByUserID(row["UserID"].ToString());
            cbIsReqGood.Checked = Convert.ToBoolean(row["IsRequestGood"]);
            lbCreateDate.Text = Convert.ToDateTime(row["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm");
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        string reqId = Request.Form["RequisitionID"];
        if(sender==null)
            reqId = Request["RequisitionID"];
        if (string.IsNullOrEmpty(reqId))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        hfAddOrEdit.Value = "View";
        hfRequisitionID.Value = reqId;
        string sql = string.Format(cmdText + " AND T.RequisitionID='" + reqId + "' ", this.TableName);
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql, this.TableName); 
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0];
        ResetComponent();
        pnlEdit.Visible = true;
        btnSelect.Visible = false;
        btnSave.Visible = false;
        btnGetOut.Visible = false;
        btnSearchGoodCode.Visible = false;

        SetReadOnly(this.DefaultPrex, true);
        SessionManager.DataSet = ds;
        this.FillValueByPrexFromDataRow(dr, this.DefaultPrex);
        P_LoginName.Text = dr["UserName"].ToString();
        P_Department.Text = dr["OrgName"].ToString();

        GoodItemOut.IsReadOnly = true;
        GoodItemOut.InitDataOut("IC_RequisitionItem", "RequisitionItemID", "RequisitionID", reqId);
        //显示受理信息 
        DataSet tempds = PMSAccess.GetDataSet(this.Connection, "select * from PMS_TaskLX where TaskID in(select TaskID from PMS_TaskItem where TaskItemId='" + dr["TaskID"].ToString() + "')");
        if (tempds != null && tempds.Tables[0].Rows.Count > 0)
        {
            pnlTaskInfo.Visible = true;
            DataRow row = tempds.Tables[0].Rows[0];
            UserAccess user = new UserAccess(base.Connection);
            tbGuest.Text = row["Guest"].ToString();
            tbRoomNo.Text = row["RoomNo"].ToString();
            cbHouseNo.Text = row["HouseNo"].ToString();
            tbPhone.Text = row["Phone"].ToString();
            tbTroubleDescription.Text = row["TroubleDescription"].ToString();
            lbOrgID.Text = user.GetUserOrgName(row["OrgID"].ToString());
            lbUserID.Text = user.GetUserNameByUserID(row["UserID"].ToString());
            cbIsReqGood.Checked = Convert.ToBoolean(row["IsRequestGood"]);
            lbCreateDate.Text = Convert.ToDateTime(row["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm");
        }
        InitGridReceiverView(dr["TaskID"].ToString());
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hfAddOrEdit.Value == "Add")
        {
            ExecuteAdd(); 
        }
        else
        {
            ExecuteEdit();
        }
    }

    private void ExecuteAdd()
    {
        string result = GoodItemOut.PreProcessSave();
        if (!string.IsNullOrEmpty(result))
        {
            tipMessage1.ShowMessage(result, 5000);
            return;
        }

        DataRow dr = SessionManager.DataSet.Tables[this.TableName].NewRow();
        dr["RequisitionID"] = System.Guid.NewGuid().ToString();
        dr["RequisitionCode"] = PMSAccess.GetCode(EnumCodeKind.LingLiao);
        hfRequisitionID.Value = dr["RequisitionID"].ToString();
        dr["Status"] = PMSGoodsState.NotOut;
        this.FillDataRowByPrex(ref dr, this.DefaultPrex, EnumOperator.ADD);
        dr["LoginName"] = SessionManager.UserID;
        dr["Department"] = SessionManager.OrgID;

        SessionManager.DataSet.Tables[this.TableName].Rows.Add(dr);
        foreach (DataRow _dr in SessionManager.DataSet.Tables["IC_RequisitionItem"].Rows)
        {
            _dr["RequisitionID"] = hfRequisitionID.Value;
        } 
        PMSAccess.UpdateDataSet(this.Connection, new string[] { "IC_Requisition", "IC_RequisitionItem" }, SessionManager.DataSet);
        ResetComponent();
        SessionManager.DataSet.Tables.Remove("IC_RequisitionItem");
        //GoodItemOut.InitDataOut("IC_RequisitionItem", "RequisitionItemID", "RequisitionID", hfRequisitionID.Value);
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        pnlEdit.Visible = false;
        pnlGrid.Visible = true;
        tipMessage1.ShowMessage("新建成功", 5000);
    }


    private void ExecuteEdit()
    {
        string result =  GoodItemOut.PreProcessSave();
        if (!string.IsNullOrEmpty(result))
        {
            tipMessage1.ShowMessage(result, 5000);
            return;
        }
        //DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfRequisitionID.Value);
        //DataRow dr = ds.Tables[0].Rows[0];
        DataRow dr = SessionManager.DataSet.Tables[this.TableName].Rows[0];
        this.FillDataRowByPrex(ref dr, this.DefaultPrex, EnumOperator.EDIT);
        dr["LoginName"] = SessionManager.UserID;
        dr["Department"] = SessionManager.OrgID;
        DataTable dt = SessionManager.DataSet.Tables["IC_RequisitionItem"];
        foreach (DataRow row in dt.Rows)
        {
            if (row.RowState == DataRowState.Deleted) continue;
            row["RequisitionID"] = dr["RequisitionID"];
        }
        PMSAccess.UpdateDataSet(this.Connection, new string[] { "IC_Requisition", "IC_RequisitionItem" }, SessionManager.DataSet);
        tipMessage1.ShowMessage("修改成功", 5000);
    }
    public void btnSelect_Click(object sender, EventArgs e)
    {
        if (GoodItemOut.OnSelect != null)
            GoodItemOut.OnSelect(sender, e);
    }

    public void btnReceive_Click(object sender, EventArgs e)
    {
        string id = Request["RequisitionID"];
        if (string.IsNullOrEmpty(id))
        {
            base.Alert("请选择一条领料单记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }

        string sql = string.Format(cmdText + " AND T.RequisitionID='" + id + "' ", this.TableName);
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql, this.TableName);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0];
        int status = Convert.ToInt32(dr["Status"]);
        if (status == PMSGoodsState.IsOut)
        {
            Alert("您选择的申请单已经出库，不能再进行操作！");
            return;
        }

        base.WriteLog("领料申请单", "取出领料申请单中的物料'" + id + "'!",
            LogType.Warning, this.GetType().ToString());
        //修改领料单状态为已经收，经办人，经办部门
        //sql = "update " + TableName + " set status='" + PMSGoodsState.IsOut + "', LoginName='" + SessionManager.UserID + "', Department='" + SessionManager.OrgID + "' where RequisitionID='" + id + "'";
        //PMSAccess.ExecuteNonQuery(this.Connection, sql);

        ResetComponent();

        hfAddOrEdit.Value = "Receive";
        hfRequisitionID.Value = id;

        pnlEdit.Visible = true;
 
        SessionManager.DataSet = ds;

        SetReadOnly(this.DefaultPrex, false);
        this.FillValueByPrexFromDataRow(dr, this.DefaultPrex);
        P_LoginName.Text = SessionManager.Username;
        P_Department.Text = SessionManager.OrgName;
        P_GetDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

        P_LoginName.ReadOnly = true;
        P_Department.ReadOnly = true;
 
        GoodItemOut.IsReadOnly = false;
        GoodItemOut.InitDataOut("IC_RequisitionItem", "RequisitionItemID", "RequisitionID", hfRequisitionID.Value);

        //显示受理信息 
        DataSet tempds = PMSAccess.GetDataSet(this.Connection, "select * from PMS_TaskLX where TaskID in(select TaskID from PMS_TaskItem where TaskItemId='" + dr["TaskID"].ToString() + "')");
        if (tempds != null && tempds.Tables[0].Rows.Count > 0)
        {
            pnlTaskInfo.Visible = true;
            DataRow row = tempds.Tables[0].Rows[0];
            UserAccess user = new UserAccess(base.Connection);
            tbGuest.Text = row["Guest"].ToString();
            tbRoomNo.Text = row["RoomNo"].ToString();
            cbHouseNo.Text = row["HouseNo"].ToString();
            tbPhone.Text = row["Phone"].ToString();
            tbTroubleDescription.Text = row["TroubleDescription"].ToString();
            lbOrgID.Text = user.GetUserOrgName(row["OrgID"].ToString());
            lbUserID.Text = user.GetUserNameByUserID(row["UserID"].ToString());
            cbIsReqGood.Checked = Convert.ToBoolean(row["IsRequestGood"]);
            lbCreateDate.Text = Convert.ToDateTime(row["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm");
        }
        InitGridReceiverView(dr["TaskID"].ToString());
    }

    public void btnGetOut_Click(object sender, EventArgs e)
    { 
        string result = GoodItemOut.PreProcessSave();
        if (!string.IsNullOrEmpty(result))
        {
            tipMessage1.ShowMessage(result, 5000);
            return;
        }

        string id = hfRequisitionID.Value;
        DataRow rdr = null;
        if (hfAddOrEdit.Value == "Add")
        {
            id =hfRequisitionID.Value= System.Guid.NewGuid().ToString();
            PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, "select * from IC_Requisition where 1=2 ", "IC_Requisition");
            rdr = SessionManager.DataSet.Tables["IC_Requisition"].NewRow();
            rdr["RequisitionID"] = id;
            rdr["RequisitionCode"] = PMSAccess.GetCode(EnumCodeKind.LingLiao);
            rdr["status"] = PMSGoodsState.NotOut;
            //this.FillDataRowByPrex(rdr, this.DefaultPrex);
            rdr["LoginName"] = SessionManager.UserID;
            rdr["Department"] = SessionManager.OrgID;
            SessionManager.DataSet.Tables["IC_Requisition"].Rows.Add(rdr);
        }
        else if (hfAddOrEdit.Value == "Edit" || hfAddOrEdit.Value == "Receive")
        {
            //添加领料单的领料人信息
            PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, "select * from IC_Requisition where RequisitionID='" + id + "'", "IC_Requisition");
            rdr = SessionManager.DataSet.Tables["IC_Requisition"].Rows[0];
        }
 
        int status = Convert.ToInt32(rdr["Status"]);
        if (status == PMSGoodsState.IsOut)
        {
            tipMessage1.ShowMessage("您选择的申请单已经出库，不能再进行操作！", 5000);
            return;
        }
        //修改领料单状态为已经签发
        rdr["status"] = PMSGoodsState.IsOut;
        rdr["RequisitionDate"] = P_RequisitionDate.Text;
        rdr["RequisitionUser"] = P_RequisitionUser.Text;
        rdr["RequisitionType"] = P_RequisitionType.SelectedValue;
        rdr["GetUser"] = P_GetUser.Text;
        if (!string.IsNullOrEmpty(P_GetDate.Text.Trim()))
        {
            rdr["GetDate"] = P_GetDate.Text;
        }
        rdr["LoginName"] = SessionManager.UserID;
        rdr["Department"] = SessionManager.OrgID;
        rdr["Description"] = P_Description.Text;
        //处理实际签发数量，并且将信息保存到出库表中
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, "select * from IC_StockOut where 1=2 ", "IC_StockOut");
        PMSAccess.FillDataSet(this.Connection, SessionManager.DataSet, "select * from IC_StockOutItem where 1=2 ", "IC_StockOutItem");
        
        //出库单ID
        string outId = System.Guid.NewGuid().ToString();
        //添加出库单
        DataRow out_dr = SessionManager.DataSet.Tables["IC_StockOut"].NewRow();
        out_dr["StockOutID"] = outId;
        out_dr["StockOutCode"] = PMSAccess.GetCode(EnumCodeKind.ChuKu);
        out_dr["RequisitionID"] = id;
        //设置出库单未还回状态
        out_dr["Status"] = StockOutStatus.UnBack;
        out_dr["StockOutType"] = P_RequisitionType.SelectedValue;
        out_dr["StockOutDate"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        out_dr["LoginName"] = SessionManager.UserID;
        out_dr["Department"] = SessionManager.OrgID;
        out_dr["Description"] = P_Description.Text;
        PMSAccess.AppendValue(ref out_dr, EnumOperator.ADD);
        SessionManager.DataSet.Tables["IC_StockOut"].Rows.Add(out_dr);
        //添加具体出库项
        DataTable dt = SessionManager.DataSet.Tables["IC_RequisitionItem"];
        DataTable _dt = SessionManager.DataSet.Tables["IC_StockOutItem"];
        DataTable _gooddt = SessionManager.DataSet.Tables["IC_Goods"];
        DataRow[] _gooddrs = null;
        foreach (DataRow dr in dt.Rows)
        {
            if (dr.RowState == DataRowState.Deleted) continue;
            dr["RequisitionID"] = id;
            DataRow _dr = _dt.NewRow();
            _dr["StockOutItemID"] = System.Guid.NewGuid().ToString();
            _dr["StockOutID"] = outId;
            _dr["RequisitionID"] = id;
            _dr["GoodsID"] = dr["GoodsID"];
            _dr["GoodsCode"] = dr["GoodsCode"];
            _dr["Subject"] = dr["Subject"];
            _dr["CategoryID"] = dr["CategoryID"];
            _dr["GoodsMaker"] = dr["GoodsMaker"];
            _dr["TypeModel"] = dr["TypeModel"];
            _dr["GoodsUnit"] = dr["GoodsUnit"];
            _dr["GoodsCount"] = dr["GoodsCount"];
            _dr["Description"] = dr["Description"];
            _dr["CreateTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            _dr["CreateUser"] = SessionManager.UserID;
            _dt.Rows.Add(_dr);
            //修改库存量:可用库存量和实际库存量
            _gooddrs = _gooddt.Select("GoodsID='" + dr["GoodsID"].ToString() + "'");
            if (_gooddrs.Length > 0)
            {
                _gooddrs[0]["ActiveCount"] = Convert.ToInt32(_gooddrs[0]["ActiveCount"]) - Convert.ToInt32(dr["GoodsCount"]);
                _gooddrs[0]["RealCount"] = Convert.ToInt32(_gooddrs[0]["RealCount"]) - Convert.ToInt32(_dr["GoodsCount"]);
            }
        }

        PMSAccess.UpdateDataSet(this.Connection, new string[] { "IC_Requisition", "IC_RequisitionItem", "IC_StockOut", "IC_StockOutItem", "IC_Goods" }, SessionManager.DataSet);
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        tipMessage1.ShowMessage("成功出库", 5000);
    }

    #region 显示领单人信息
    protected void InitGridReceiverView(string taskId)
    {
        string cmdItemsText = @"SELECT T.*,A.Username,A.GroupCode,A.Mobile,B.OrgID,D.Subject as OrgName,C.CodeName as GroupName  FROM PMS_TaskUser T inner join PMS_User A on A.UserID = T.UserID 
                    left join T_OrgUser B on B.UserID = T.UserID 
                    left join  T_CodeConfig C on C.CodeValue=A.GroupCode  AND C.Category = '" + PMSCodeCategory.PMS_BMZB + @"' 
                    left join T_OrgTree D on D.OrgID=B.OrgID where 1=1 and T.UserType='" + PMSCodeCategory.UserReceiver + "' and T.TaskID='" + taskId + "'";

        DataSet ds = PMSAccess.GetDataSet(this.Connection, cmdItemsText);
        gridReceiverView.DataSource = ds.Tables[0];
        gridReceiverView.ShowFooter = false;
        gridReceiverView.DataBind();
    }
    #endregion
}
